#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int longestValidParentheses(char* s);

int main()
{
	return 0;
}

int longestValidParentheses(char* s)
{
	int left = 0, right = 0, maxlength = 0, length = 0;
	for (; s[length]; length++) {
		if (s[length] == '(') left++;
		else right++;
		if (left == right) maxlength = fmax(maxlength, left << 1);
		else if (right > left) left = right = 0;
	}

	left = right = 0;
	for (int i = length - 1; i >= 0; i--) {
		if (s[i] == '(') left++;
		else right++;
		if (left == right) maxlength = fmax(maxlength, left << 1);
		else if (left > right) left = right = 0;
	}

	return maxlength;
}
